﻿using CustomCharacter;
using CustomCharacter.Ability;
using Exiled.Events.EventArgs.Interfaces;
using InventorySystem;
using InventorySystem.Items.Firearms;
using InventorySystem.Items.Firearms.Attachments;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using CustomCharacter.Interfaces;
using UnityEngine;
using Player = Exiled.API.Features.Player;

namespace Scp703
{
    [RoleInfo("SCP-703", PlayerRoles.RoleTypeId.Scientist, PlayerRoles.RoleTypeId.Scientist,
        MaxHealth = 100)]
    public class Scp703 : CustomCharacter.CustomCharacter, IScpNumber, IRoundStartSpawn, ITrialCard
    {
        public Scp703(int id) : base(id)
        {
        }

        public int Cost => 350;

        [Description("计时器")]
        public int Timer
        {
            get
            {
                var ab = Player.GetAbility("Scp703_Attr_1");
                if (ab.Ability == null)
                    return -1;
                var data = ab.Datas;
                return (int)data["tminus"];
            }
        }

        public override Color32 RoleColor => new Color32(65, 105, 255, 255);

        public string ScpNumber => "703";

        public byte MinPlayers => 6;

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new AttributeAbility("Scp703_Attr_1", "每3分钟获得一个随机物品", Attribute_1,
    true, null, new KeyValuePair<string, object>[]
    {
        new KeyValuePair<string, object>("tminus", 0)
    }, GetType())
        };
        }

        private static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (trigger == AbilityTrigger.Update)
            {
                var ab = player.GetAbility("Scp703_Attr_1");
                if (ab.Ability == null)
                    return new AbilityOutput(false);
                var data = ab.Datas;
                int tminus = (int)data["tminus"];
                tminus--;
                if (tminus <= 0)
                {
                    Array types = Enum.GetValues(typeof(ItemType));
                    var item = player.Inventory.ServerAddItem((ItemType)types.GetValue(UnityEngine.Random.Range(0, types.Length)));
                    if (item is Firearm firearm)
                    {
                        if (AttachmentsServerHandler.PlayerPreferences.TryGetValue(player.ReferenceHub, out Dictionary<ItemType, uint> dictionary) && dictionary.TryGetValue(firearm.ItemTypeId, out uint code))
                        {
                            firearm.ApplyAttachmentsCode(code, true);
                        }
                        FirearmStatusFlags firearmStatusFlags = FirearmStatusFlags.MagazineInserted;
                        if (firearm.HasAdvantageFlag(AttachmentDescriptiveAdvantages.Flashlight))
                        {
                            firearmStatusFlags |= FirearmStatusFlags.FlashlightEnabled;
                        }
                        firearm.Status = new FirearmStatus(firearm.AmmoManagerModule.MaxAmmo, firearmStatusFlags, firearm.GetCurrentAttachmentsCode());
                    }
                    data["tminus"] = 180;
                    return new AbilityOutput(true, "已刷新物品");
                }
                data["tminus"] = tminus;
            }
            return new AbilityOutput(false);
        }
    }
}